# Library
library(plyr)
library(foreign)

# Read in the data
dat <- read.csv("Raw_experiments.csv")

# age 
dat$age_dec <- as.numeric(as.character(dat$age))*0.1

# gender
dat$female <- ifelse(dat$profile_gender=="Female",1,0)

# education
dat$educ_fourlevels <- as.numeric(mapvalues(as.character(dat$profile_education_level),
                                  c("No formal qualifications",                            
                                  "Youth training certificate/skillseekers",        
                                  "Recognised trade apprenticeship completed",       
                                  "Clerical and commercial",                              
                                  "City & Guilds certificate" ,                           
                                  "City & Guilds certificate - advanced" ,                
                                  "ONC" ,                                                 
                                  "CSE grades 2-5" ,                                      
                                  "CSE grade 1, GCE O level, GCSE, School Certificate" ,  
                                  "Scottish Ordinary/ Lower Certificate" ,                
                                  "GCE A level or Higher Certificate" ,                   
                                  "Scottish Higher Certificate"   ,                       
                                  "Nursing qualification (e.g. SEN, SRN, SCM, RGN)"  ,    
                                  "Teaching qualification (not degree)"  ,                
                                  "University diploma"   ,                                
                                  "University or CNAA first degree (e.g. BA, B.Sc, B.Ed)",
                                  "University or CNAA higher degree (e.g. M.Sc, Ph.D)"  , 
                                  "Other technical, professional or higher qualification",
                                  "Don't know"  ,                                         
                                  "Prefer not to say"), 
                                  c(1,1,1,1,1,1,1,1,1,1,2,2,3,3,3,3,4,NA,NA,NA)))

# income
dat$income <- as.numeric(mapvalues(as.character(dat$xprofile_gross_household),
                                 c ("under £5,000 per year" ,       
                                    "£5,000 to £9,999 per year" ,   
                                    "£10,000 to £14,999 per year" , 
                                     "£15,000 to £19,999 per year"  ,
                                     "£20,000 to £24,999 per year"  ,
                                    "£25,000 to £29,999 per year" , 
                                    "£30,000 to £34,999 per year" , 
                                     "£35,000 to £39,999 per year" , 
                                     "£40,000 to £44,999 per year" , 
                                     "£45,000 to £49,999 per year" , 
                                     "£50,000 to £59,999 per year" , 
                                    "£60,000 to £69,999 per year"  ,
                                    "£70,000 to £99,999 per year"  ,
                                     "£100,000 to £149,999 per year",
                                     "£150,000 and over"    ,        
                                     "Don't know"        ,           
                                    "Prefer not to answer") ,
                                  c(1:15,NA,NA)))

# Organize moral conviction
dat$mc1 <- as.numeric(mapvalues(as.character(dat$Oxford4_1), c("Not at all","Slightly","Moderately","Very","Extremely"),
                                   1:5))
dat$mc2 <- as.numeric(mapvalues(as.character(dat$Oxford4_2), c("Not at all","Slightly","Moderately","Very","Extremely"),
                                   1:5))

# Make average moral conviction measure
dat$mc_avg <- apply(dat[,c("mc1","mc2")],1,mean)

# Organize moral foundations
dat$mft1care1 <- as.numeric(mapvalues(as.character(dat$Oxford5_1), 
                                      c("Not all relevant (This consideration has nothing to do with my judgements of right and wrong)",
                                        "Not very relevant",
                                        "Slightly relevant",
                                        "Somewhat relevant",
                                        "Very relevant",
                                        "Extremely relevant (This is one of the most important factors when I judge right and wrong)"),
                                      1:6)) 
dat$mft1fair1 <- as.numeric(mapvalues(as.character(dat$Oxford5_2), 
                                      c("Not all relevant (This consideration has nothing to do with my judgements of right and wrong)",
                                        "Not very relevant",
                                        "Slightly relevant",
                                        "Somewhat relevant",
                                        "Very relevant",
                                        "Extremely relevant (This is one of the most important factors when I judge right and wrong)"),
                                      1:6)) 
dat$mft1care2 <- as.numeric(mapvalues(as.character(dat$Oxford5_4), 
                                      c("Not all relevant (This consideration has nothing to do with my judgements of right and wrong)",
                                        "Not very relevant",
                                        "Slightly relevant",
                                        "Somewhat relevant",
                                        "Very relevant",
                                        "Extremely relevant (This is one of the most important factors when I judge right and wrong)"),
                                      1:6)) 
dat$mft1fair2 <- as.numeric(mapvalues(as.character(dat$Oxford5_5), 
                                      c("Not all relevant (This consideration has nothing to do with my judgements of right and wrong)",
                                        "Not very relevant",
                                        "Slightly relevant",
                                        "Somewhat relevant",
                                        "Very relevant",
                                        "Extremely relevant (This is one of the most important factors when I judge right and wrong)"),
                                      1:6)) 

dat$mft2care1 <- as.numeric(mapvalues(as.character(dat$Oxford6_1), 
                                     c("Strongly disagree","Moderately disagree",
                                       "Slightly disagree","Slightly agree",
                                       "Moderately agree","Strongly agree"),
                                     1:6)) 
dat$mft2fair1 <- as.numeric(mapvalues(as.character(dat$Oxford6_2), 
                                         c("Strongly disagree","Moderately disagree",
                                           "Slightly disagree","Slightly agree",
                                           "Moderately agree","Strongly agree"),
                                         1:6)) 
dat$mft2care2 <- as.numeric(mapvalues(as.character(dat$Oxford6_4), 
                                         c("Strongly disagree","Moderately disagree",
                                           "Slightly disagree","Slightly agree",
                                           "Moderately agree","Strongly agree"),
                                         1:6))
dat$mft2fair2 <- as.numeric(mapvalues(as.character(dat$Oxford6_5), 
                                         c("Strongly disagree","Moderately disagree",
                                           "Slightly disagree","Slightly agree",
                                           "Moderately agree","Strongly agree"),
                                         1:6)) 

# Create moral foundations variables
dat$mftcare <- apply(dat[,c("mft1care1","mft1care2","mft2care1","mft2care2")],1,mean)
dat$mftfair <- apply(dat[,c("mft1fair1","mft1fair2","mft2fair1","mft2fair2")],1,mean)

# Create variables indicating those who paid attention
dat$mft1_attention <- as.numeric(mapvalues(as.character(dat$Oxford5_3), 
                                         c("Not all relevant (This consideration has nothing to do with my judgements of right and wrong)",
                                           "Not very relevant",
                                           "Slightly relevant",
                                           "Somewhat relevant",
                                           "Very relevant",
                                           "Extremely relevant (This is one of the most important factors when I judge right and wrong)"),
                                         c(1,1,1,1,0,0)))
dat$mft2_attention <- as.numeric(mapvalues(as.character(dat$Oxford6_3), 
                                              c("Strongly disagree","Moderately disagree",
                                                "Slightly disagree","Slightly agree",
                                                "Moderately agree","Strongly agree"),
                                              c(0,0,0,1,1,1))) 

# Organize pre-treatment like variables
dat$likeL_pre <- as.numeric(mapvalues(as.character(dat$Oxford7_L), 
                                           c("0 – Strongly dislike",
                                             "1","2","3","4","5","6","7","8","9",
                                             "10 – Strongly like"),
                                           0:10)) 
dat$likeC_pre <- as.numeric(mapvalues(as.character(dat$Oxford7_C), 
                                           c("0 – Strongly dislike",
                                             "1","2","3","4","5","6","7","8","9",
                                             "10 – Strongly like"),
                                           0:10)) 

# Organize post-treatment like variables
dat$likeL_post <- as.numeric(mapvalues(as.character(dat$Oxford8_L), 
                                           c("0 – Strongly dislike",
                                             "1","2","3","4","5","6","7","8","9",
                                             "10 – Strongly like"),
                                           0:10)) 
dat$likeC_post <- as.numeric(mapvalues(as.character(dat$Oxford8_C), 
                                           c("0 – Strongly dislike",
                                             "1","2","3","4","5","6","7","8","9",
                                             "10 – Strongly like"),
                                           0:10))

# Create differenced like variables
dat$likeLdiff <- dat$likeL_post - dat$likeL_pre
dat$likeCdiff <- dat$likeC_post - dat$likeC_pre

# Split the data to Lab survey and Con survey
dat_lab <- subset(dat, !is.na(vignettesplit_L))
dat_con <- subset(dat, !is.na(vignettesplit_C))

# Create leaner indicators
dat_lab$leaners_lab <- ifelse(dat_lab$Oxford2=="Labour",1,0)
dat_con$leaners_con <- ifelse(dat_con$Oxford2=="Conservative",1,0)

# Create treatment variables
dat_lab$mrcontrol <- as.numeric(mapvalues(as.character(dat_lab$vignettesplit_L), c("1","2","3"),c(0,1,NA)))
dat_lab$mrpr <- as.numeric(mapvalues(as.character(dat_lab$vignettesplit_L), c("1","2","3"),c(NA,1,0)))
dat_lab$prcontrol <- as.numeric(mapvalues(as.character(dat_lab$vignettesplit_L), c("1","2","3"),c(0,NA,1)))

dat_con$mrcontrol <- as.numeric(mapvalues(as.character(dat_con$vignettesplit_C), c("1","2","3"),c(0,1,NA)))
dat_con$mrpr <- as.numeric(mapvalues(as.character(dat_con$vignettesplit_C), c("1","2","3"),c(NA,1,0)))
dat_con$prcontrol <- as.numeric(mapvalues(as.character(dat_con$vignettesplit_C), c("1","2","3"),c(0,NA,1)))

# Create pre-treatment dislike variables
dat_lab$dislikeL_pre <- mapvalues(dat_lab$likeL_pre,0:10,10:0)
dat_con$dislikeC_pre<- mapvalues(dat_con$likeC_pre,0:10,10:0)

# Keep only variables needed for analyses
dat_lab <- dat_lab[,c("mrcontrol","mrpr","likeL_pre","likeL_post","likeLdiff","mftcare","mftfair","dislikeL_pre","prcontrol",
                      "leaners_lab","female","age_dec","educ_fourlevels","income","mc_avg","mft1_attention","mft2_attention")]
dat_con <- dat_con[,c("mrcontrol","mrpr","likeC_pre","likeC_post","likeCdiff","mftcare","mftfair","dislikeC_pre","prcontrol",
                      "leaners_con","female","age_dec","educ_fourlevels","income","mc_avg","mft1_attention","mft2_attention")]

# Save the data
save(dat_lab, dat_con, file="Data_experiments.RData")
